Skip to content

Editorial: Introduce an operation for backing To{Int,Uint}{8,16,32} and To{BigInt,BigUint}64#3832

Open
gibson042 wants to merge 6 commits intotc39:mainfrom
gibson042:2026-04-tofixedsizeinteger
Open

Editorial: Introduce an operation for backing To{Int,Uint}{8,16,32} and To{BigInt,BigUint}64#3832
gibson042 wants to merge 6 commits intotc39:mainfrom
gibson042:2026-04-tofixedsizeinteger

Conversation

@gibson042
Copy link
Copy Markdown
Member

@gibson042 gibson042 commented Apr 12, 2026

Ref #3792

As suggested in #3792, ToFixedSizeInteger provides a single place to comment on the invariants of fixed-size integer conversion. However, in this PR it joins the supported operations in the existing Type Conversion section rather than splitting them out into a new subsection.

Comment thread spec.html Outdated
Comment on lines +5323 to +5325
_signed_: ~unsigned~ or ~signed~,
_bitWidth_: a positive integer,
_type_: ~number~ or ~bigint~,
Copy link
Copy Markdown
Member Author

@gibson042 gibson042 Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three arguments are partially redundant with what TypedArray element type describes in a single enumeration, but the latter also includes FLOAT{16,32,64} and I couldn't find a clean way to layer separation of just the INT/BIGINT values so I've left that as a potential exercise for the future (but ideas for the present are nonetheless welcome).

I suspect that going that route would involve something like introducing ToFloat{16,32,64} operations, migrating the last four columns of that table into Abstract Operations along with Is{Unsigned,UnclampedInteger,BigInt}ElementType for use by ToFixedSizeInteger, and simplifying e.g. NumericToRawBytes/NumericToRawBytes/etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three separate abstract operations seem a bit nicer, because it more clearly separates conversion from and to Number resp. BigInt.

ToFixedSizeInteger (
  _argument_: an integer,
  _signed_: ~unsigned~ or ~signed~,
  _bitWidth_: a positive integer,
): an integer
  1. Let _fixedInt_ be _int_ modulo 2<sup>_bitWidth_</sup>.
  1. If _signed_ is ~signed~ and _fixedInt_ ≥ 2<sup>_bitWidth_ - 1</sup>, return _fixedInt_ - 2<sup>_bitWidth_</sup>.
  1. Return _fixedInt_.


ToFixedSizeNumber(
  _argument_: an ECMAScript language value,
  _signed_: ~unsigned~ or ~signed~,
  _bitWidth_: a positive integer,
): either a normal completion containing an integral Number or a throw completion
  1. Let _int_ be ? ToIntegerOrInfinity(_argument_).
  1. If _int_ is not finite, return 0.
  1. Return 𝔽(ToFixedSizeInteger(_int_, _signed_, _bitWidth_)).


ToFixedSizeBigInt(
  _argument_: an ECMAScript language value,
  _signed_: ~unsigned~ or ~signed~,
  _bitWidth_: a positive integer,
): either a normal completion containing a BigInt or a throw completion
  1. Let _int_ be ℝ(? ToBigInt(_argument_)).
  1. Return ℤ(ToFixedSizeInteger(_int_, _signed_, _bitWidth_)).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I took a half step in this direction, keeping the single new operation but making it infallibly map extended mathematical values and moving initial conversion to call sites.

@github-actions
Copy link
Copy Markdown

The rendered spec preview for this PR is available as a single page at https://tc39.es/ecma262/pr/3832 and as multiple pages at https://tc39.es/ecma262/pr/3832/multipage .

@gibson042 gibson042 added the editor call to be discussed in the next editor call label Apr 13, 2026
Comment thread spec.html
ToInt32 (
_argument_: an ECMAScript language value,
): either a normal completion containing an integral Number or a throw completion
ToFixedSizeInteger (
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this now performs no type conversion, "IntegerToFixedSize" might be a better name. Opinions are welcome.

@gibson042 gibson042 changed the title Editorial: Introduce ToFixedSizeInteger for backing To{Int,Uint}{8,16,32} and To{BigInt,BigUint}64 Editorial: Introduce an operation for backing To{Int,Uint}{8,16,32} and To{BigInt,BigUint}64 Apr 16, 2026
Comment thread spec.html Outdated
Copy link
Copy Markdown
Member

@michaelficarra michaelficarra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise. Great readability improvement.

@michaelficarra michaelficarra removed the editor call to be discussed in the next editor call label Apr 23, 2026
@michaelficarra michaelficarra self-requested a review April 27, 2026 21:46
Comment thread spec.html Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer us to retain something like the example from the old note. Something like ToFixedSizeInteger(ToFixedSizeInteger(_x_, ~signed~, 32), ~unsigned~, 32) is the same as ToFixedSizeInteger(_x_, ~unsigned~, 32).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@michaelficarra michaelficarra added the ready to merge Editors believe this PR needs no further reviews, and is ready to land. label Apr 30, 2026
Comment thread spec.html Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. If _int_ is either +∞ or -∞, return 0.

(editorial convention)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear (despite GitHub bug), my suggestion is to insert "either".

Copy link
Copy Markdown
Member Author

@gibson042 gibson042 May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the comparison conventions terribly confusing/incomplete and possibly contradictory, but since int is a mathematical value, shouldn't this actually be If _int_ = +∞ or _int_ = -∞, return 0. as seen at BinaryOr, BigIntBitwiseOp, and IsWordChar? I almost always see the "is either $A or $B" form with $A and $B being language values and spec enums.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, it's an 'extended mathematical value', which the comparison conventions don't mention, but lumping it in with mathematical values would be reasonable. So yeah, using = seems more consistent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

editorial change ready to merge Editors believe this PR needs no further reviews, and is ready to land.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants